package com.example.app.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Component
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "czn.auth")
public class AuthConfig {
    public final static String USER_INFO="auth_user_info";
    public static final String OPEN_ID = "auth_user_openid";
    public static final String ACCESS_TOKEN = "auth_user_accessToken";


    /**
     * 重定向地址
     * 跳转网页系统页面地址
     * 例如:https://www.baidu.com 当前系统访问域名
     * 默认是首页
     */
    private String redirectUrl;
    /**
     * AppID APPid
     */
    private String appId;
    /**
     * AppSecret
     */
    private String appSecret;
    /**
     * 认证网关
     */
    private String authHost;
    /**
     * 授权登录
     */
    private String authUrl="/h5/auth";
    /**
     * 获取信息网关
     */
    private String authRest="/rest/";
    /**
     *
     */
    /**
     * 返回参数
     */
    private String responseType="code";

    public String getRedirectUrl() {
        if(StringUtils.isEmpty(redirectUrl)){
            final HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
            redirectUrl= request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
        }
        return redirectUrl;
    }

    public String getAuthHost() {
        if (StringUtils.isEmpty(authHost)) {
            throw new NullPointerException("请配置认证服务器地址");
        }
        return authHost.endsWith("/")?authHost.substring(0,authHost.length()-1):authHost;
    }

    public void setAuthHost(String authHost) {
        this.authHost = authHost;
    }

    public void setRedirectUrl(String redirectUrl) {
        this.redirectUrl = redirectUrl;
    }

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getAppSecret() {
        return appSecret;
    }

    public void setAppSecret(String appSecret) {
        this.appSecret = appSecret;
    }

    public String getResponseType() {
        return responseType;
    }

    public void setResponseType(String responseType) {
        this.responseType = responseType;
    }

    public String getAuthUrl() {
        return getAuthHost()+authUrl;
    }

    public void setAuthUrl(String authUrl) {
        this.authUrl = authUrl;
    }

    public String getAuthRest() {
        return getAuthHost()+authRest;
    }

    public void setAuthRest(String authRest) {
        this.authRest = authRest;
    }
}
